
$(document).ready(function() {
    $('#areas')[0].onchange();
    doubleLabel();
});

function doubleLabel(){
    var row = $('#areas').parent().parent().clone()[0];
    row.children[0].children[0].innerText = 'Disease:';
    var elem = row.children[1].children[0];
    var elem2 = row.children[1].children[1];
    var specialities = row.children[1].children[2];
    var specialityId = $($('#areas')[0].parentNode.parentNode.children[1].children[2]).val();
    var elem3 = row.children[1].children[3];
    elem.parentNode.removeChild(elem);
    elem2.parentNode.removeChild(elem2);
    elem3.parentNode.removeChild(elem3);
    $('#areas').parent().parent().after(row);
    $($('#areas')[0].parentNode.parentNode.children[1].children[2]).remove();
    $(specialities).val(specialityId);
    areasTemp = specialities;
}


function validate() {
    jQuery.validator.addMethod("valueNotEquals", function(value, element, arg){
        return value != arg;
    });

    jQuery.validator.addMethod('required_group', function(val, el) {
        var $module = $(el).parents('table');
        return $module.find('.required_group:filled').length;
    });

    jQuery.validator.addClassRules('required_group', {
        'required_group' : true
    });

    jQuery.validator.messages.required_group = '*';

    $("#formEdit")
        .validate(
        {
            rules : {
                'investigatorId' : {
                    required:true,
                    valueNotEquals:"-1"
                }
            },
            messages : {
                'investigatorId': {
                    valueNotEquals: "*"
                }
            }
        })
}

function addRow(url) {

    contadorParaNuevasFilas++;
    var counts = contadorParaNuevasFilas;

    var rowDrug = $($('tr#drug:last')).clone()[0];
    rowDrug.children[0].children[0].style.display = 'none';
    rowDrug.children[1].children[0].value = '';
    rowDrug.children[1].children[0].name = 'protocol.drogas['+ counts +'].id';
    rowDrug.children[2].children[0].onclick = function(){deleteRow(this); return false;};

    $('tr#drug:last').after(rowDrug);
}

function deleteRow(anchor) {

    var row = $(anchor).parent().parent();

    if($('tr#drug').length == 1){
        row[0].children[1].children[0].value = -1;
    }else{
        $(row).remove();
    }
    $('tr#drug:first')[0].children[0].children[0].style.display = '';
    $('tr#drug:first')[0].children[1].children[0].name = 'protocol.drogas[0].id';

}

function beforeSaveProtocol(){
    $('#protocolId').attr('disabled', false);
}